package com.jy.datapipeline.common.support;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.jy.datapipeline.common.commoninterface.EasyExcelExecutionStrategy;
import com.jy.datapipeline.common.listener.ReadExcelTemplateListener;
import com.jy.datapipeline.export.entity.RuleTemplateModel;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.stereotype.Component;

import javax.servlet.ServletOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 * @类名 ImportEasyExcelSupport
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2023/8/7 18:15
 * @版本 1.0
 */
@Component
public class EasyExcelSupport {

    public void generateRuleTemplate(ServletOutputStream outputStream) {
        //背景为白色
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //边框颜色为灰色
        headWriteCellStyle.setBottomBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
        headWriteCellStyle.setTopBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
        headWriteCellStyle.setLeftBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
        headWriteCellStyle.setRightBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
        //字体大小为8
        WriteFont writeFont = new WriteFont();
        writeFont.setFontHeightInPoints((short)10);
        headWriteCellStyle.setWriteFont(writeFont);
        //添加策略
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, new WriteCellStyle());
        //进行写操作
        EasyExcel.write(outputStream, RuleTemplateModel.class)
                .registerWriteHandler(horizontalCellStyleStrategy)
                .charset(Charset.defaultCharset())
                .autoCloseStream(Boolean.TRUE)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .sheet("规则模版")
                .doWrite(ListUtils.newArrayList(new RuleTemplateModel()));
    }
    
    public <T> void analysisTemplate(InputStream inputStream, Integer batchCount, EasyExcelExecutionStrategy strategy, Class<T> clazz) {
        EasyExcel
                .read(inputStream, clazz, new ReadExcelTemplateListener<>(batchCount, strategy))
                .sheet().doRead();
    }
}
